#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); 
void Print( List L ); 

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    L1 = Read();
    L2 = Read();
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}

List Read(){
	int N,m;
	scanf("%d",&N);
	List A=(List)malloc(sizeof(struct Node));
	A->Next=NULL;
	List p=A;
	for(int i=0;i<N;i++){
		List u=(List)malloc(sizeof(struct Node));
		scanf("%d",&m);
		p->Next=u;
		u->Data=m;
		p=u;
	} 
	p->Next=NULL;
	return A;
} 
void Print( List L ){
	List p=L->Next;
	if(p==NULL)
		printf("NULL");
	else{
		List o=L;
		while(p!=NULL){
			printf("%d ",p->Data);
			p=p->Next;
	    }
	}
	printf("\n");
}
List Merge( List L1, List L2 ){
	List m,n,k;
	List L=(List)malloc(sizeof(struct Node));
	L->Next=NULL;
	m=L1->Next;
	n=L2->Next;
	k=L;
	while(m!=NULL&&n!=NULL){
		if(m->Data<=n->Data){
			k->Next=m;
			k=k->Next;
			m=m->Next;
		}else{
			k->Next=n;
			k=k->Next;
			n=n->Next;
		}
	}
	if(m!=NULL){
		k->Next=m;
	}else{
		k->Next=n; 
	}
	L1->Next=NULL;
	L2->Next=NULL;
	return L;
}
